class Solution:
    def maxUncrossedLines(self, nums1: list, nums2: list) -> int:
        m = len(nums1)
        n = len(nums2)
        dp = [[0 for _ in range(m + 1)] for _ in range(n + 1)]
        for i in range(1, m + 1):
            for j in range(1, n + 1):
                if nums1[i - 1] == nums2[j - 1]:
                    dp[i][j] = 1 + dp[i - 1][j - 1]
                else:
                    dp[i][j] = max(dp[i][j - 1], dp[i - 1][j])

        return dp[-1][-1]
